var app = getApp();
Page({

  data: {
    info:[],
    userInfo: null,
    title:'',
    zhicheng:'',
    fid:'9999999',
    page:'',
    itemList: ['入库', '出库'],
    status:'点击选择状态',
    choose:'',
    
  },

  onLoad: function (options) {
    //var fid = options.scene;
    var param = options.scene.split("_");
    console.log(param)
     var fid = param[0];
     var page = param[1];

    //var fid = 439;
    this.setData({ fid: fid});
    this.setData({ page: page});
    this.getlist(fid,page)
  
  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{

      this.setData({ userInfo:wx.getStorageSync('userInfo')});

      this.setData({ title:wx.getStorageSync('title')});

      if(wx.getStorageSync('gerenziliao')){

        this.setData({ userinfo:wx.getStorageSync('gerenziliao')});
      }

    }
  },

  
  changezt(){
    var that = this
    wx.showActionSheet({
      itemList: ['入库', '出库'],
      success(res) {
        console.log(res)
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex});
 
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  formSubmit: function (event) {
    var that = this;
    var choose = that.data.choose;
    if(choose === ''){
      app.error('请选择状态');
      return;
    }

    //如果出库 可以修改出仓数量
    if(choose == 1){
      console.log(event.detail.value.cpnum);
      console.log(event.detail.value.num);

      if(event.detail.value.cpnum>0){
          if(parseInt(event.detail.value.cpnum) > parseInt(event.detail.value.num)){
            app.error('出仓数量不能大于当前数量');
            return;
          }
      }else{
        app.error('请输入正确的出仓数量');
        return;
      }
    }

    wx.request({
      url: app.apiUrl + '/api/data/cpcadd',
      data: {
        id: that.data.fid,
        page:that.data.page,
        groupid:wx.getStorageSync('groupid'),
        openid:wx.getStorageSync('openid'),
        choose:choose,
        cpnum:event.detail.value.cpnum
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
    
       if(res.data.code == 1){

        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        });
        
        setTimeout(function () {
          wx.navigateTo({
            url: '/page/my/index',
          });
        }, 2000)
       }else{
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        });
       }

      }
    })

  },

  getlist:function (fid,page){
    var that = this
    wx.request({
      url: app.apiUrl + '/api/data/cpcinfo',
      data: {
        fid:fid,
        page:page,
        openid:wx.getStorageSync('openid'),
        groupid:wx.getStorageSync('groupid')
        
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
        if(res.data.code == 1){
          that.setData({ info: res.data.data});
              
        }else{
          wx.showModal({
            title: '温馨提示',
               content: res.data.msg,
              showCancel: false,
              success:function(res){
              wx.redirectTo({
                url: '/page/my/index',
              })
              }
            });


        }
     
      }
    });
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})